<template>
  <div class="common-layout">
    <el-container>
      <el-aside width="200px">
        <!-- logo -->
        <div class="el-aside-logo"></div>
        <!-- 菜单分类 -->
        <el-menu default-active="/article/category" background-color="#1f1f1f" text-color="#fff"
          active-text-color="#008c8c" router>
          <el-menu-item index="/article/category">
            <el-icon>
              <management />
            </el-icon>
            <span>文章分类</span>
          </el-menu-item>
          <el-menu-item index="/article/manage">
            <el-icon>
              <promotion />
            </el-icon>
            <span>文章管理</span>
          </el-menu-item>
          <el-sub-menu index="/user">
            <template #title>
              <el-icon>
                <userFilled />
              </el-icon>
              <span>个人中心</span>
            </template>
            <el-menu-item index="/user/info">
              <el-icon>
                <User />
              </el-icon>
              基本资料</el-menu-item>
            <el-menu-item index="/user/avatar">
              <el-icon>
                <Crop />
              </el-icon>
              更换头像</el-menu-item>
            <el-menu-item index="/user/resetPassword">
              <el-icon>
                <EditPen />
              </el-icon>
              重置密码</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <div style="float: left;">开发者：<strong>梁之之</strong></div>
          <!-- 下拉菜单 -->
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              <!-- 头像 -->
              <el-avatar :src="userStore.userInfo.userPic ? userStore.userInfo.userPic: avatar" />
              <!-- <el-avatar :src="avatar" /> -->
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item :icon="User" @click="router.push('/user/info')">基本信息</el-dropdown-item>
                <el-dropdown-item :icon="Crop" @click="router.push('/user/avatar')">更换头像</el-dropdown-item>
                <el-dropdown-item :icon="EditPen" @click="router.push('/user/resetPassword')">重置密码</el-dropdown-item>
                <el-dropdown-item :icon="RemoveFilled" @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-header>
        <el-main>
          <!-- TODO:问题(当路径发生变化时界面却未跳转)=>强制刷新 -->
          <router-view></router-view>
        </el-main>
        <el-footer>大事件&copy;2025-4-27 Created by 梁之之</el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { userInfoApi } from '@/api/user';
import { UserFilled, ArrowDown, Promotion, Management, User, Crop, EditPen, RemoveFilled } from '@element-plus/icons-vue';
import { onMounted, ref } from 'vue';
import { useUserStore } from '@/stores/useUserStore';
import router from '@/router';
import { REMOVE_TOKEN } from '@/utils/token';


const userStore = useUserStore()
const avatar = ref()
onMounted(async () => {
  let res = await userInfoApi()
  console.log('layout-res.data:', res.data)
  userStore.setUserInfo(res.data)
  // console.log('layout-初始信息：', userStore.printUserInfo())
  avatar.value = userStore.userInfo.userPic
  console.log('初始avatar:',avatar.value)
  if(!avatar.value){
    avatar.value = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
  }
  console.log('处理后avatar:',avatar.value)
})
// 退出登录
const logout = ()=>{
  // 清除token+返回登录页面
  REMOVE_TOKEN()
  router.replace('/login')
}
</script>

<style scoped lang="scss">
.common-layout {
  height: 100vh;
  // background-color: #666;

  .el-aside {
    height: 100vh;
    color: #fff;
    background-color: #1f1f1f;

    // &:表示其父【.el-aside】
    &-logo {
      height: 70px;
      margin-top: 10px;
      background: url(../assets/logo.png) no-repeat center;
    }
  }

  .el-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80px;
    // line-height: 80px;
    background-color: aquamarine;

    // &-acount {
    //   height: 80px;
    //   float: right;
    //   margin-right: 10px;
    //   cursor: pointer;
    //   line-height: 80px;
    // }

    .el-dropdown-link {
      display: flex;
      align-items: center;
    }
  }

  .el-main {
    background-color: aqua;
  }

  .el-footer {
    height: 80px;
    line-height: 80px;
    background-color: skyblue;
    text-align: center;
  }
}
</style>